home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / maximus / mul100.zip / MAKUSER.SCR < prev    next >
Text File  |  1993-02-01  |  13KB  |  348 lines

  1.  
  2. // MAKUSER.SCR  --  Add a defined record to Maximus Base  --  Version 1.00
  3. //
  4. // Script program for MUL - the Maximus User Language
  5. // MUL is (C) Copyright 1990-93 by CodeLand Australia
  6.  
  7. // Originally written for David Keating of 1:249/201, MAKUSER appends
  8. // or updates a record in the Maximus user base. The record information
  9. // used for the new or updated record is obtained from an external
  10. // text file, presumably created by another utility. See the example
  11. // MAKUSER.DAT file provided with this script. Following is David's
  12. // input file specification, as implimented in this script with the
  13. // read_data () function.
  14.  
  15. // INPUT TEXT FILE KEYWORDS:
  16. //
  17. //      NAME (their name)
  18. //      PASSWORD (xxxxxxx)
  19. //      CITY (city,prov)
  20. //      PHONE (xxx-xxx-xxxx)
  21. //      ALIAS (whatever)
  22. //      ACCESS (Limited, etc.)
  23. //      KEYS (1-8,A-X)
  24. //      EXPIRY (date, time)
  25. //      EXPIRYDATE (mm/dd/yy)
  26. //      EXPIRYACTION (demote,hangup)
  27. //      DEMOTE (level to demote to)
  28.  
  29. // MAKUSER supports one optional command line parameter MODIFY, which
  30. // forces the record modification mode, rather than the default
  31. // append mode. MAKUSER return a status of zero on success, and the
  32. // following error levels when trouble arises. Note that MUL will return
  33. // to DOS the exact value the script returns. Thus it's best as below,
  34. // to avoid the first 16 values, which are reserved for errors internal
  35. // to MUL itself. See the documentation for further details.
  36.  
  37. // MAKUSER return status: 0 - Success
  38. //                        17 - Input file not found
  39. //                        18 - User file not found
  40. //                        19 - User file append failed
  41. //                        20 - User file write failed
  42. //                        21 - Modification record NOT found
  43. //                        22 - Modification password mismatch
  44. //                        23 - Input name or password not found
  45. //                        24 - Append record already exists
  46.  
  47. char *ufile = "USER.BBS";                   // Path & name of Maximus user file
  48. char *ifile = "MAKUSER.DAT";                // Path & name of input file
  49.  
  50. char *banner = "MAKUSER v1.00";             // Script banner
  51. char *desc = "Append a new user record";    // Description
  52.  
  53. // Input fields
  54. char usrname[36];                           // (their name)
  55. char usrpwd[16];                            // (xxxxxxx)
  56. char usrcity[36];                           // (city,prov)
  57. char usrphone[15];                          // (xxx-xxx-xxxx)
  58. char usralias[21];                          // (whatever)
  59. int usrpriv;                                // (Limited, etc.)
  60. long usrkeys;                               // (1-8,A-X)
  61. int usrexpiry;                              // (date, time)
  62. int usrexpiryaction;                        // (demote,hangup)
  63. int usrxpdate;                              // (mm/dd/yy)
  64. int usrxppriv;                              // (level to demote to)
  65.  
  66. int modifyflag=0;                           // Modify existing record flag
  67.  
  68. int status=0;                               // Script return status
  69. long fp;                                    // Text record input file handle
  70.  
  71. main (int argc, char *arg1)                 // Main program
  72. {
  73.     printf ("\n%s - %s\n\n",banner,desc);   // Announce
  74.     getcmdline (argc,arg1);                 // Get the command line
  75.  
  76.     if (process_input ()) update_base ();   // Process data and append record
  77.  
  78.     saybibi ();
  79.     exit (status);                          // Return status
  80. }
  81.  
  82. process_input ()                            // Process the input data file
  83. {
  84.     int count;
  85.  
  86.     if (!fp=fopen (ifile,"rt")) {
  87.         printf ("ERROR opening input file %s\n",ifile);
  88.         status=17; return 0;
  89.     }
  90.  
  91.     // Notify
  92.     printf ("Reading input file \"%s\" ",ifile);
  93.  
  94.     count=read_data ();                     // Parse the input file
  95.     fclose (fp);                            // Close the input file
  96.     putch ('\n');                           // Notify cleanup
  97.  
  98.     if (count<2) {                         // Name & Password are required
  99.         status=23;
  100.         return 0;                          
  101.     }
  102.  
  103.     // Notify
  104.     printf ("Input record: %s from %s\n",usrname,usrcity);
  105.  
  106.     return count;
  107. }
  108.  
  109. update_base ()                              // Add the new record
  110. {
  111.     int found;
  112.  
  113.     if (!BaseOpen (ufile)) {
  114.         printf ("\nERROR opening user file %s\n",ufile);
  115.         status=18; return 0;
  116.     }
  117.  
  118.     // Notify
  119.     printf ("Updating user file \"%s\" ",ufile);
  120.  
  121.     found=find_record ();                   // Check for existance
  122.  
  123.     if (modifyflag) {                       
  124.  
  125.         // Modify existing record
  126.         if (found) {                        // The record must exist
  127.             if (!stricmp (usrpwd,USRpwd)) { // Passwords must match
  128.                 // Transfer new data
  129.                 if (usrcity[0]) strcpy (USRcity,usrcity);
  130.                 if (usrphone[0]) strcpy (USRphone,usrphone);
  131.                 if (usralias[0]) strcpy (USRalias,usralias);
  132.                 if (usrpriv) USRpriv=usrpriv;
  133.                 if (usrkeys) USRkeys=usrkeys;
  134.                 if (usrexpiry) USRxpflag=Or (usrexpiry,usrexpiryaction);
  135.                 if (usrxppriv) USRxppriv=usrxppriv;
  136.                 if (usrxpdate) USRxpdate=usrxpdate;
  137.                 if (!BaseWrite (found)) {
  138.                     status=20;
  139.                     printf ("\nERROR base write failed");
  140.                 }
  141.             }
  142.             else {
  143.                 status=22;
  144.                 printf ("\nERROR Modification password mismatch");
  145.             }
  146.         }
  147.         else {
  148.             status=21;
  149.             printf ("\nERROR Modification record NOT found");
  150.         }
  151.  
  152.     }
  153.     else {                                  
  154.  
  155.         // Append new record
  156.         if (found) {                        // The record must NOT exist
  157.             status=24;
  158.             printf ("\nERROR Append record already exists");
  159.         }
  160.         else {
  161.             BaseClear ();                   // Clear record buffer
  162.             strcpy (USRname,usrname);       // Transfer data to record buffer
  163.             strcpy (USRpwd,usrpwd);
  164.             strcpy (USRcity,usrcity);
  165.             strcpy (USRphone,usrphone);
  166.             strcpy (USRalias,usralias);
  167.             USRpriv=usrpriv;
  168.             USRkeys=usrkeys;
  169.             USRxpflag=Or (usrexpiry,usrexpiryaction);
  170.             USRxppriv=usrxppriv;
  171.             USRxpdate=usrxpdate;
  172.             if (!BaseAppend ()) {
  173.                 status=19;
  174.                 printf ("\nERROR base append failed");
  175.             }
  176.         }
  177.  
  178.     }
  179.  
  180.     BaseClose ();                           // Close the user base
  181.     putch ('\n');                           // Notify cleanup
  182. }
  183.  
  184. find_record ()                              // Check for record existance
  185. {
  186.     int rec;
  187.  
  188.     for (rec=1;rec<=BaseCount ();++rec) {
  189.         if (!BaseRead (rec)) break;
  190.         if (!stricmp (usrname,USRname)) {   // If names match
  191.             return rec;                     // Return success
  192.         }
  193.     }
  194.  
  195.     return 0;
  196. }
  197.  
  198. read_data ()                                // Parse the input file
  199. {
  200.     int verbs=0;                            // Required verbs processed count
  201.     char *p, *q, line[256];
  202.  
  203.     while (fgets (line,256,fp)) {           // Read a line from text file
  204.  
  205.         p=line; while (isspace (*p)) p++;   // Skip leading white space
  206.         strip_nl (p);                       // Remove trailing NL & whitespace
  207.         if (strlen (p) && p[0]!=';') {      // If a valid line
  208.             // printf("\"%s\"\n",p);        // Debug
  209.             q=p;
  210.             while(*q&&!isspace (*q)) q++;   // Find token ending
  211.             if (*q&&q!=p) {                 // If we have keyword plus data
  212.                 *q='\0'; q++;               // Point q at data
  213.                 while (isspace (*q)) q++;   // Skip leading white space
  214.  
  215.                 // Process the keyword
  216.                 if (!strnicmp ("NAME",p,strlen(p))) {
  217.                     ++verbs;
  218.                     strncpy (usrname,q,35); usrname[35]='\0';
  219.                 }
  220.                 else if (!strnicmp ("PASSWORD",p,strlen(p))) {
  221.                     ++verbs;
  222.                     strncpy (usrpwd,q,15); usrpwd[15]='\0';
  223.                 }
  224.                 else if (!strnicmp ("CITY",p,strlen(p))) {
  225.                     strncpy (usrcity,q,35); usrcity[35]='\0';
  226.                 }
  227.                 else if (!strnicmp ("PHONE",p,strlen(p))) {
  228.                     strncpy (usrphone,q,15); usrphone[15]='\0';
  229.                 }
  230.                 else if (!strnicmp ("ALIAS",p,strlen(p))) {
  231.                     strncpy (usralias,q,20); usralias[20]='\0';
  232.                 }
  233.                 else if (!strnicmp ("ACCESS",p,strlen(p))) {
  234.                     usrpriv=set_access (q);
  235.                 }
  236.                 else if (!strnicmp ("KEYS",p,strlen(p))) {
  237.                     usrkeys=set_keys (q);
  238.                 }
  239.                 else if (!strnicmp ("EXPIRY",p,strlen(p))) {
  240.                     if (!strnicmp ("DATE",q,strlen(q))) {
  241.                         usrexpiry=XP_DATE;
  242.                     }
  243.                     else if (!strnicmp ("TIME",q,strlen(q))) {
  244.                         usrexpiry=XP_TIME;
  245.                     }
  246.                     else printf ("\nERROR: Unknown expiry type - \"%s\"\n",q);
  247.                 }
  248.                 else if (!strnicmp ("EXPIRYDATE",p,strlen(p))) {
  249.                     usrxpdate=StrToDate (q);
  250.                 }
  251.                 else if (!strnicmp ("EXPIRYACTION",p,strlen(p))) {
  252.                     if (!strnicmp ("DEMOTE",q,strlen(q))) {
  253.                         usrexpiryaction=XP_DEMOTE;
  254.                     }
  255.                     else if (!strnicmp ("HANGUP",q,strlen(q))) {
  256.                         usrexpiryaction=XP_HANGUP;
  257.                     }
  258.                     else printf ("\nERROR: Unknown expiry action - \"%s\"\n",q);
  259.                 }
  260.                 else if (!strnicmp ("DEMOTE",p,strlen(p))) {
  261.                     usrxppriv=set_access (q);
  262.                 }
  263.                 else printf ("\nERROR: Unrecognised keyword - \"%s\"\n",p);
  264.  
  265.             }
  266.             else {                          // Check for non-data keywords
  267.                 if (!strnicmp ("MODIFY",p,strlen(p))) {
  268.                     ++modifyflag;
  269.                 }
  270.                 else printf ("ERROR: Unrecognised keyword - \"%s\"\n",p);
  271.             }
  272.  
  273.         }
  274.     }
  275.  
  276.     return verbs;
  277. }
  278.  
  279. set_access (char *str)                      // Return binary priv from str priv
  280. {
  281.     if (!strnicmp ("TWIT",str,4))       return TWIT;
  282.     if (!strnicmp ("DISGRACE",str,8))   return DISGRACE;
  283.     if (!strnicmp ("LIMITED",str,7))    return LIMITED;
  284.     if (!strnicmp ("NORMAL",str,6))     return NORMAL;
  285.     if (!strnicmp ("WORTHY",str,6))     return WORTHY;
  286.     if (!strnicmp ("PRIVIL",str,6))     return PRIVIL;
  287.     if (!strnicmp ("FAVORED",str,7))    return FAVORED;
  288.     if (!strnicmp ("EXTRA",str,5))      return EXTRA;
  289.     if (!strnicmp ("CLERK",str,5))      return CLERK;
  290.     if (!strnicmp ("ASSTSYSOP",str,9))  return ASSTSYSOP;
  291.     if (!strnicmp ("SYSOP",str,5))      return SYSOP;
  292.     if (!strnicmp ("HIDDEN",str,6))     return HIDDEN;
  293.  
  294.     return DISGRACE;
  295. }
  296.  
  297. set_keys (char *str)                         // Return binary keys from key str
  298. {
  299.     long keybit;
  300.     char *p=str;
  301.  
  302.     USRkeys=0;
  303.     while (*p) {
  304.         if (isdigit (*p)) {
  305.             // Get key bit
  306.             keybit = ShiftLeft (1,*p-49);
  307.             BaseKeyOn (keybit);              // Turn key on
  308.         }
  309.         else if (isalpha (*p)) {
  310.             // Get key bit
  311.             keybit = ShiftLeft (1,toupper (*p)-57);
  312.             BaseKeyOn (keybit);              // Turn key on
  313.         }
  314.         p++;
  315.     }
  316.     return USRkeys;
  317. }
  318.  
  319. strip_nl (char *str)                        // Remove trailing NL & whitespace
  320. {
  321.     char *p;
  322.  
  323.     p=str+strlen(str)-1;                    // Point at last character
  324.  
  325.     while (p!=str) {
  326.         if(!isspace (*p) || *p!='\n') break;
  327.         p--;
  328.     }
  329.  
  330.     if(isspace (*p) || *p=='\n') *p='\0';  // Terminate string
  331.     else p[1]='\0';
  332. }
  333.  
  334. getcmdline (int argc, char *arg1)           // Get the command line
  335. {
  336.     // Check for the MODIFY command line option
  337.     if (argc) if (!stricmp (arg1,"MODIFY")) ++modifyflag;
  338. }
  339.  
  340. // Byebye
  341. saybibi ()
  342. {                             
  343.     puts ("\nMakUser done!\n");
  344. }
  345.  
  346. // End of script
  347.  
  348.